/*****************************************************************************/
/**
*
* @file key.h
*
* Header file for key.c.
* Target device: STM32F103RET6.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  01/29/2020 Created
* </pre>
*
*------------------------------------------------------------------------------
* Author:	abu
*					E-mail:	abu_liu@opencores.org
*------------------------------------------------------------------------------
*
******************************************************************************/
#ifndef KEY_H		/* prevent circular inclusions */
#define KEY_H		/* by using protection macros */

#ifdef __cplusplus
	extern "C" {
#endif

/***************************** Include Files *********************************/
#include "stm32f10x.h"

/************************** Constant Definitions *****************************/
#define KEY_ON   0
#define KEY_OFF  1

#define KEY2_PORT	GPIOC
#define KEY3_PORT	GPIOC
#define KEY4_PORT	GPIOD
#define KEY5_PORT	GPIOC

#define KEY2_PIN GPIO_Pin_11
#define KEY3_PIN GPIO_Pin_12
#define KEY4_PIN GPIO_Pin_2
#define KEY5_PIN GPIO_Pin_13

#define KEY2_SCAN() KeyScan(KEY2_PORT,KEY2_PIN)
#define KEY3_SCAN() KeyScan(KEY3_PORT,KEY3_PIN)
#define KEY4_SCAN() KeyScan(KEY4_PORT,KEY4_PIN)
#define KEY5_SCAN() KeyScan(KEY5_PORT,KEY5_PIN)

#define KEY2_PRESSED	(KEY2_SCAN()==KEY_ON)
#define KEY3_PRESSED	(KEY3_SCAN()==KEY_ON)
#define KEY4_PRESSED	(KEY4_SCAN()==KEY_ON)
#define KEY5_PRESSED	(KEY5_SCAN()==KEY_ON)

//interrupt related macros
#define	KEY2_INT_EXTI_PORTSOURCE	GPIO_PortSourceGPIOC
#define	KEY3_INT_EXTI_PORTSOURCE	GPIO_PortSourceGPIOC
#define	KEY4_INT_EXTI_PORTSOURCE	GPIO_PortSourceGPIOD
#define	KEY5_INT_EXTI_PORTSOURCE	GPIO_PortSourceGPIOC
#define	KEY2_INT_EXTI_PINSOURCE		GPIO_PinSource11
#define	KEY3_INT_EXTI_PINSOURCE		GPIO_PinSource12
#define	KEY4_INT_EXTI_PINSOURCE		GPIO_PinSource2
#define	KEY5_INT_EXTI_PINSOURCE		GPIO_PinSource13
#define	KEY2_INT_EXTI_LINE				EXTI_Line11
#define	KEY3_INT_EXTI_LINE				EXTI_Line12
#define	KEY4_INT_EXTI_LINE				EXTI_Line2
#define	KEY5_INT_EXTI_LINE				EXTI_Line13
#define	KEY2_INT_EXTI_IRQ					EXTI15_10_IRQn
#define	KEY3_INT_EXTI_IRQ					EXTI15_10_IRQn
#define	KEY4_INT_EXTI_IRQ					EXTI2_IRQn
#define	KEY5_INT_EXTI_IRQ					EXTI15_10_IRQn

/************************** Function Declarations ******************************/
void key_init(void);
void Key4_Intr_Init(void);
uint8_t KeyScan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void KEY2_IRQHandler(void);
void KEY3_IRQHandler(void);
void KEY4_IRQHandler(void);
void KEY5_IRQHandler(void);

		
#ifdef __cplusplus
}
#endif

#endif
